home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / src / net / bind-contrib.tar.gz / bind-contrib.tar / contrib / umich / dnsstats next >
Text File  |  1996-10-25  |  11KB  |  304 lines

  1. #!/bin/sh
  2. PATH=:/bin:/usr/bin:/usr/ucb:/usr/local/bin
  3. # -------------------------------------------------------------
  4. #  Copyright (c) 1989, 1993, 1994
  5. #  Regents of the University of Michigan.  All rights reserved.
  6. #
  7. #  Redistribution and use is permitted provided that this notice 
  8. #  is preserved and that due credit is given to the University of 
  9. #  Michigan. The name of the University may not be used to endorse 
  10. #  or promote products derived from this software without specific 
  11. #  prior written permission. This software is provided "as is" 
  12. #  without express or implied warranty.
  13. #
  14. #  DNS Statistics gatherer
  15. #  Author:  Bryan Beecher
  16. #  Last Modified:   6/14/94
  17. #
  18. #  To make use of this software, you need to be running a copy of
  19. #  BIND 4.9 (or later) compiled with the QRYLOG option defined.
  20. #
  21. #  The assumption behind this script is that it will be run out
  22. #  of crontab daily just before some sort of syslog manager
  23. #  copies the current contents of LOGFILE elsewhere before
  24. #  emptying the LOGFILE.  However, it can certainly be run on
  25. #  a LOGFILE that is not emptied daily, and in this case it
  26. #  would merely report the cumulative statistics.
  27. # -------------------------------------------------------------
  28.  
  29. # -------------------------------------------------------------
  30. #  C O N F I G U R A T I O N    S E C T I O N
  31. # -------------------------------------------------------------
  32.  
  33. ##
  34. ##  NOTE:  Ultrix users may want to change the first line
  35. ##         of this script from /bin/sh to /bin/sh5.
  36. ##
  37.  
  38. # -------------------------------------------------------------
  39. #  Do we use 'getopts' or 'getopt'?
  40. # -------------------------------------------------------------
  41. GETOPT=getopts
  42.  
  43. # -------------------------------------------------------------
  44. #  This is the name of the log.
  45. # -------------------------------------------------------------
  46. LOGFILE=/var/log/named
  47.  
  48. # -------------------------------------------------------------
  49. #  This is the program we use to go from an IP addr to host
  50. # -------------------------------------------------------------
  51. QUERYPROG="/usr/local/bin/dig -x"
  52.  
  53. # -------------------------------------------------------------
  54. #  This is the awk we use.
  55. # -------------------------------------------------------------
  56. AWK=/bin/awk
  57.  
  58. # -------------------------------------------------------------
  59. #  This is the directory in which we should create temp files
  60. # -------------------------------------------------------------
  61. TMPDIR=/usr/tmp
  62.  
  63. # -------------------------------------------------------------
  64. #  This is the default number of entries we want printed in
  65. #  each category of statistics.
  66. # -------------------------------------------------------------
  67. STOPAT=25
  68.  
  69. # -------------------------------------------------------------
  70. #  E N D    O F    C O N F I G U R A T I O N    S E C T I O N
  71. # -------------------------------------------------------------
  72.  
  73. # -------------------------------------------------------------
  74. #  handy files
  75. # -------------------------------------------------------------
  76. TMPFILE=$TMPDIR/.dnsstats$$
  77.  
  78. ADDRFILE=$TMPDIR/.addrs$$
  79. NAMEFILE=$TMPDIR/.names$$
  80. TYPEFILE=$TMPDIR/.types$$
  81. WEEKFILE=$TMPDIR/.week$$
  82. TOTALFILE=$TMPDIR/.total$$
  83.  
  84. # -------------------------------------------------------------
  85. #  handle arguments
  86. # -------------------------------------------------------------
  87. #       -d <day>
  88. #       This flag is used to append a dot-day suffix to the LOGFILE.
  89. #       Handy where log files are kept around for the last week
  90. #       and contain a day suffix.
  91. #
  92. #       -f <logfile>
  93. #       Change the LOGFILE value altogether.
  94. #
  95. #       -n
  96. #       Don't try to resolve IP addresses from in-addr.arpa names
  97. #       to "regular" names.  Handy if the DNS is slow or you just
  98. #       don't care about the domain names associated with the IP
  99. #       addresses.
  100. #
  101. #       -w
  102. #       Count up all of the DNS statistics for the whole week.
  103. #
  104. #       -c <#>
  105. #       Print only the top-# of entries in each category.
  106. #       Default is $STOPAT. 
  107. #
  108. #       -a
  109. #       Print the entire list of entries in each category.
  110. # -------------------------------------------------------------
  111. NONAMES=0
  112. PRINTALL=0
  113.  
  114. trap "rm -f $TMPFILE $ADDRFILE $NAMEFILE $TYPEFILE $WEEKFILE $TOTALFILE; exit 0" 0 1 2 3 15
  115.  
  116. gethostbyaddr() {
  117.         $QUERYPROG $ADDRESS | $AWK ' BEGIN {
  118.             msg = " ** Query failed ** "
  119.             }
  120.             $3 == "PTR" {
  121.                 msg = substr($4, 1, length($4) - 1)
  122.             }
  123.             END {
  124.                 printf(" %6d  %-39s [%s]\n", count, msg, address)
  125.             }' count=$COUNT address=$ADDRESS - ;
  126. }
  127.  
  128. if [ $GETOPT = "getopts" ] ; then
  129.         while getopts ac:d:f:nw ARG ; do
  130.                 case $ARG in
  131.                         a)      PRINTALL=1
  132.                                 ;;
  133.                         c)      STOPAT=$OPTARG
  134.                                 ;;
  135.                         d)      LOGFILE=$LOGFILE"."$OPTARG
  136.                                 ;;
  137.                         f)      LOGFILE=$OPTARG
  138.                                 ;;
  139.                         n)      NONAMES=1
  140.                                 ;;
  141.                         w)      cat $LOGFILE* > $WEEKFILE
  142.                                 LOGFILE=$WEEKFILE
  143.                                 ;;
  144.  
  145.                 esac
  146.         done
  147.         shift `expr $OPTIND - 1`
  148. else
  149.         set -- `getopt ac:d:f:nw $*`
  150.         if [ $? != 0 ] ; then
  151.                 exit 2
  152.         fi
  153.         for ARG in $* ; do
  154.                 case $ARG in
  155.                         -a)     PRINTALL=1
  156.                                 shift
  157.                                 ;;
  158.                         -c)     STOPAT=$2
  159.                                 shift 2
  160.                                 ;;
  161.                         -d)     LOGFILE=$LOGFILE"."$2
  162.                                 shift 2
  163.                                 ;;
  164.                         -f)     LOGFILE=$2
  165.                                 shift 2
  166.                                 ;;
  167.                         -n)     NONAMES=1
  168.                                 shift
  169.                                 ;;
  170.                         -w)     cat $LOGFILE* > $WEEKFILE
  171.                                 LOGFILE=$WEEKFILE
  172.                                 shift
  173.                                 ;;
  174.                         --)     shift
  175.                                 break
  176.                                 ;;
  177.                 esac
  178.         done
  179. fi
  180.  
  181. # -------------------------------------------------------------
  182. #  divide the log file into three files:
  183. #       one for source addrs of incoming querys
  184. #       one for domain names that were queried upon
  185. #       one for query types
  186. # -------------------------------------------------------------
  187. $AWK '
  188. {
  189.         if ((n == 0) && ($5 == "last"))
  190.                 next
  191.         else if ($5 == "last") {
  192.                 total += $8
  193.                 sender[info[2]] += $8
  194.                 query[info[3]] += $8
  195.                 querytype[info[4]] += 8
  196.         }
  197.         else if ($6 == "XX") {
  198.                 n = split($0, info, "/")
  199.                 if (n == 4) {
  200.                         total++
  201.                         sender[info[2]]++
  202.                         query[info[3]]++
  203.                         querytype[info[4]]++
  204.                 }
  205.         }
  206.         else {
  207.                 n = 0
  208.         }
  209. }
  210. END {
  211.         if (total > 0) {
  212.                 for (i in sender)
  213.                         print sender[i], i >f1
  214.                 for (i in query)
  215.                         print query[i], i >f2
  216.                 for (i in querytype)
  217.                         print querytype[i], i >f3
  218.                 print total >f4
  219.         }
  220. }' f1=$ADDRFILE f2=$NAMEFILE f3=$TYPEFILE f4=$TOTALFILE $LOGFILE
  221.  
  222. # -------------------------------------------------------------
  223. #  Bail out if info was not produced
  224. # -------------------------------------------------------------
  225. if [ ! -s $ADDRFILE -a -s $NAMEFILE -a -s $TYPEFILE -a -s $TOTALFILE ] ; then
  226.         echo "No data on which to operate"
  227.         rm -f $TMPFILE $ADDRFILE $NAMEFILE $TYPEFILE $WEEKFILE $TOTALFILE
  228.         exit 0
  229. fi
  230.  
  231. # -------------------------------------------------------------
  232. #  Print some general information
  233. # -------------------------------------------------------------
  234. echo "DNS stats for" `hostname` "for period ending" `ls -l $LOGFILE | $AWK '{ print $5, $6, $7 }'`
  235. echo "Total queries received: " `cat $TOTALFILE`
  236. echo
  237. echo "Part I -- query sources"
  238. echo
  239.  
  240. # -------------------------------------------------------------
  241. #  First, tell who was querying this nameserver
  242. # -------------------------------------------------------------
  243. if [ $NONAMES -eq 0 ] ; then
  244.         echo " Number   Source (by name if available)           IP address"
  245.         echo " ------   -----------------------------           ----------"
  246.         sort -rn $ADDRFILE > $TMPFILE
  247.         if [ $PRINTALL -eq 1 ] ; then
  248.                 mv $TMPFILE $ADDRFILE
  249.         else
  250.                 head -$STOPAT $TMPFILE > $ADDRFILE
  251.         fi
  252.         while [ 1 ] ; do
  253.                 read COUNT ADDRESS
  254.                 if [ $? -ne 0 ] ; then
  255.                         break
  256.                 fi
  257.                 gethostbyaddr
  258.         done < $ADDRFILE
  259. else
  260.         echo " Number  IP address"
  261.         echo " ------  ----------"
  262.         sort -rn $ADDRFILE > $TMPFILE
  263.         if [ $PRINTALL -eq 1 ] ; then
  264.                 $AWK '{ printf(" %6d  [%s]\n", $1, $2) }' $TMPFILE
  265.         else
  266.                 head -$STOPAT $TMPFILE | $AWK '{ printf(" %5d  [%s]\n", $1, $2) }'
  267.         fi
  268. fi
  269.  
  270. # -------------------------------------------------------------
  271. #  Second, tell what names were being queried upon
  272. # -------------------------------------------------------------
  273. echo
  274. echo "Part II -- queried names"
  275. echo
  276. echo " Number  Queried name"
  277. echo " ------  ------------"
  278. sort -rn $NAMEFILE > $TMPFILE
  279. if [ $PRINTALL -eq 1 ] ; then
  280.         $AWK '{ printf(" %6d  %s\n", $1, $2) }' $TMPFILE
  281. else
  282.         head -$STOPAT $TMPFILE | $AWK '{ printf(" %6d  %s\n", $1, $2) }'
  283. fi
  284.  
  285. # -------------------------------------------------------------
  286. #  Third, tell what sort of queries there were
  287. # -------------------------------------------------------------
  288. echo
  289. echo "Part III -- query types"
  290. echo
  291. echo " Number  Type"
  292. echo " ------  ----"
  293. sort -rn $TYPEFILE > $TMPFILE
  294. if [ $PRINTALL -eq 1 ] ; then
  295.         $AWK '{ printf(" %6d  %s\n", $1, $2) }' $TMPFILE
  296. else
  297.         head -$STOPAT $TMPFILE | $AWK '{ printf(" %6d  %s\n", $1, $2) }'
  298. fi
  299.  
  300. # -------------------------------------------------------------
  301. #  Last, tidy things up
  302. # -------------------------------------------------------------
  303. rm -f $TMPFILE $ADDRFILE $NAMEFILE $TYPEFILE $WEEKFILE $TOTALFILE
  304.